﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SuperFox.SuperServiceReference;
using System.Windows.Media.Imaging;

namespace SuperFox
{
    public partial class clientWelcomePage : UserControl
    {
        
        public static readonly DependencyProperty ProductsProperty =
          DependencyProperty.Register("Products", typeof(ObservableCollection<DbProducts>), typeof(clientWelcomePage),
                new PropertyMetadata(new ObservableCollection<DbProducts>(), new PropertyChangedCallback(ProductsPropertyChanged)));


        public ObservableCollection<DbProducts> Products
        {
            get { return (ObservableCollection<DbProducts>)GetValue(ProductsProperty); }
            set { SetValue(ProductsProperty, value); }
        }

        private static void ProductsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // create the products display

            ObservableCollection<DbProducts> prods = e.NewValue as ObservableCollection<DbProducts>;

            clientWelcomePage wp = d as clientWelcomePage;

            int rowCount = 0;
            int colCount = 0;

            wp.ProductHolder.RowDefinitions.Add(new RowDefinition());

            // first add a new row to the grid
            
            foreach (DbProducts p in prods)
            {

                if (!p.Visible)
                    continue;

                clientProduct cp = new clientProduct()
                {
                    Description = p.prod_name,
                    Image = new ImageBrush()
                    {
                        ImageSource = new BitmapImage(new Uri(Config.PRODUCT_IMAGE_DIR + "/" + p.prod_smallimage, UriKind.RelativeOrAbsolute))
                    }
                };

                

                //cp.ProLink.Tag = p;
                
                // this filed is not used so i'm going to use it to hold the client name
                p.prod_shortdescription = wp.ClientNameTb.Text;

                cp.ProLink.Click += new RoutedEventHandler(ProLink_Click);

                cp.SetValue(Grid.RowProperty, rowCount);
                cp.SetValue(Grid.ColumnProperty, colCount);

                colCount++;

                if (colCount == 4)
                {
                    colCount = 0;
                    rowCount++;

                    wp.ProductHolder.RowDefinitions.Add(new RowDefinition());
                }

                cp.DataContext = p;

                wp.ProductHolder.Children.Add(cp);
                
            }
            
        }


        /// <summary>
        /// handles what happens when someone clicks on a link.
        /// 
        /// the product object is stored in the tag of the link
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void ProLink_Click(object sender, RoutedEventArgs e)
        {

            clientProductDetails cpd = new clientProductDetails()
            {
                Product = (sender as HyperlinkButton).DataContext as DbProducts
            };

            cpd.DataContext = (sender as HyperlinkButton).DataContext as DbProducts;

            cpd.ProName.Text = ((sender as HyperlinkButton).DataContext as DbProducts).prod_shortdescription;

            (App.Current.RootVisual as MainPage).LoadContent(cpd);

            
        }

        public clientWelcomePage()
        {
            InitializeComponent();

            // load the welcome message

            MessageTb.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_confirmationmessage;

            //if (Config.IN_DEBUG)
            //{
            //    (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart = new ShoppingCartCollection();

            //    Random rand = new Random();

            //    foreach (DbProducts prod in (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products)
            //    {

            //        foreach (DbProductOptionsValues val in prod.Options.Values)
            //        {
            //            val.TotalSelected = rand.Next(1, 20);
            //        }

            //        (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart.Add(prod);

            //    }
            //}

            //LayoutRoot.Background = new SolidColorBrush(Config.StringToColour((App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.BackgroundColour));

            // load the logo

            //BitmapImage img = new BitmapImage(new Uri(Config.PRODUCT_IMAGE_DIR + "/" + (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_logo, UriKind.RelativeOrAbsolute));

            //Image img = new Image()
            //{
            //    Source = new BitmapImage(new Uri(Config.PRODUCT_IMAGE_DIR + "/" + (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_logo, UriKind.RelativeOrAbsolute))
            //};

            //img.SetValue(Grid.RowProperty, 0);
            //img.SetValue(Grid.ColumnProperty, 0);
            //img.SetValue(Grid.RowSpanProperty, 2);
            //img.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Right);

            //Canvas can = new Canvas();

            //can.SetValue(Grid.RowProperty, 0);
            //can.SetValue(Grid.ColumnProperty, 0);
            //can.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Right);
            

            //can.Children.Add(img);

            //LayoutRoot.Children.Add(img);

            //ImageCanvas.Background = new ImageBrush()
            //{
            //    ImageSource = new BitmapImage(new Uri(Config.PRODUCT_IMAGE_DIR + "/" + (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_logo, UriKind.RelativeOrAbsolute))
            //};

        }
    }
}
